createQr();
var hand;
function createQr() {
    $.ajax({
        type: "GET",
        url: "createQr",
        dataType: "json",
        success: function(data){
           console.log(data);
           var qrUrl=data.data.qrUrl;
           if(qrUrl!=""){
               makeCode(qrUrl);
               hand=setInterval(function(){
                   checkQr(data.data);
               },2000)
           }

        }
    });
}

function makeCode (content) {
    var qrcode = new QRCode(document.getElementById("loginQr"), {
        width : 150,
        height : 150
    });
    qrcode.makeCode(content);
}

function checkQr(data) {
    $.ajax({
        type: "POST",
        url: "checkQr",
        data:{
           "cookies":data.cookies,
           "sId":data.sId,
           "time":data.time,
            "token":data.token
        },
        async:false,
        dataType: "json",
        success: function(data){
            console.log(data);
            var jsonObject=JSON.parse(data.msg);
            var message=jsonObject.data.message;
            if(message=='成功'){
                autoLogin(jsonObject.data.returnValue.sid);
                clearInterval(hand);
            }else if(message.indexOf('过期')!=-1){
                createQr();
                clearInterval(hand);
            }else{
                console.log(message);
            }
        }
    });
}
function autoLogin(data) {
    $.ajax({
        type: "POST",
        url: "loginQr",
        data:{
            "sId":data,
        },
        async:false,
        dataType: "json",
        success: function(data){
            console.log(data);

        }
    });
}